package com.bawei.module_equipment

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.BaseToolbarActivity
import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.lib_net.storage.utils.Gson
import com.bawei.module_equipment.databinding.ActivityEquipmentBinding
import com.bawei.module_equipment.ui.adapter.EquipmentAdapter
import com.bawei.module_equipment.ui.equipment.EquipmentViewModel
import dagger.hilt.android.AndroidEntryPoint

@Route(path = Router.Ui.EquipmentActivity)
@AndroidEntryPoint
class EquipmentActivity : BaseToolbarActivity<EquipmentViewModel,ActivityEquipmentBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_equipment
    override val mViewModel: EquipmentViewModel by viewModels()

    private val mAdapter: EquipmentAdapter by  lazy {
        EquipmentAdapter (
            onItemClick = {
                //点击item跳转到详情页
                ARouter.getInstance().build(Router.Ui.EquipmentDetailActivity).navigation()
            }
        )
    }

    override fun initView() {
        mBinding.rvEquip.adapter = mAdapter
        mBinding.rvEquip.layoutManager = LinearLayoutManager(this)
    }

    override fun initData() {
        mViewModel.inspection()

    }

    override fun initListener() {
        Log.d("TAG", "initListener: "+ Gson.toJson(mViewModel.mEquipments))
        mViewModel.equipPageFlow.observeWithLifecycle(this){
            when(it){
                is Success -> {
                    Log.d("TAG", "initListener: "+ Gson.toJson(mViewModel.mEquipments))
                    mAdapter.setData(mViewModel.mEquipments)
                    mAdapter.notifyDataSetChanged()
                }

                Empty ->{}
                is Failed -> {}
                Loading -> {

                }
            }
        }

    }



    override fun initStatusBar() {
        mBinding.appbar.toolbarTitle.text = getString(R.string.string_equip_manager)
        mBinding.appbar.toolbar.inflateMenu(com.bawei.lib_common.R.menu.single_menu)
        mBinding.appbar.toolbar.setOnMenuItemClickListener {
            when(it.itemId == com.bawei.lib_common.R.id.action){
                true -> {
                    ARouter.getInstance().build(Router.Ui.EquipmentOrderActivity).navigation()
                }

                false -> {}
            }
            true
        }
        mBinding.appbar.toolbar.setNavigationOnClickListener {
            finish()
        }
    }

}